<template>
  <item-panel class="item-panel">
    <div style="padding: 0 0 10px;border-bottom: 1px solid #ececec;line-height: 17px;"><h3>组件库</h3></div>
    <div style="padding: 10px 0;">
      <el-input v-model="searchText" placeholder="搜索组件" @input="searchWidget" clearable></el-input>
    </div>
    <el-collapse v-for="(cateItem, cateIndex) in (searchText ? searchResultList : nodeItems)" :key="cateIndex">
      <el-collapse-item :title="cateItem.name" >
        <div>
          <item v-for="(item, index) in cateItem.dataList"
            :key="index"
            :type="item.type || 'node'"
            :size="item.size || '100*30'"
            :shape="item.shape || 'flow-rect'"
            :model="{
              // must have model property
              color: item.model.color || '#1890FF',
              label: item.model.label || '未命名',
              ...item.model,
            }"
          >
            <template>
              <div :class="'item'">
                <span>{{ item.model.label || '节点' }}</span>
              </div>
            </template>
          </item>
        </div>
      </el-collapse-item>
    </el-collapse>


    
  </item-panel>
</template>

<script>
import { Item, ItemPanel } from '../../index'

export default {
  name: 'EditorItemPanel',
  components: { ItemPanel, Item },
  props: {
    // all nodes for drag to generate flowchart
    nodeItems: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      searchText: '',
      searchResultList: []
    }
  },
  methods: {
    searchWidget(txt) {

      let resultList = []
      let tempList = _.clone(this.nodeItems)
      tempList.forEach(item => {
        
        let childList = [];
        if(item.dataList && item.dataList.length > 0) {

          item.dataList.forEach(child => {
            console.log(child)
            if(child.model.label.indexOf(txt) > -1) {
              childList.push(child)
            }
        })
        }
        // 如果是按分类搜索
        if(item.name.indexOf(txt) > -1) {
          resultList.push(item)
        } else {
          if(childList.length > 0) {
            item.dataList = childList
            resultList.push(item)
          }
        }
      });
      console.log('resultList',resultList)
      this.searchResultList = resultList
    }
  }
}
</script>

<style lang="less">
.item-panel {
  // box-sizing: border-box;
  //position: fixed;
  width: 220px;
  height: 100%;
  padding: 10px;
  text-align: center;
  overflow: auto;
  border: 1px solid #ececec;
  .item {
    // display: flex;
    text-align: left;
    font-size: 12px;
    margin-right: auto;
    user-select: none;
    padding: 6px 0px;
  }
}
</style>
